package com.kikikeji.weizhuo.activity.core;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import com.kikikeji.weizhuo.util.DebugLog;


public abstract class BaseCoreFragmentActivity extends BaseCoreActivity {

    protected abstract int getFragmentContainerId();

    protected BaseCoreFragment currentFragment;

    public void pushFragmentToBackStatck(Class<?> cls, Bundle data) {
        FragmentParam param = new FragmentParam();
        param.cls = cls;
        param.data = data;
        param.addToBackStack = true;
        processFragement(param);
    }

    public void addFragment(Class<?> cls, Bundle data) {

        FragmentParam param = new FragmentParam();
        param.cls = cls;
        param.data = data;
        param.addToBackStack = false;
        processFragement(param);
    }

    public void replaceFragment(Class<?> cls, Bundle data) {

        FragmentParam param = new FragmentParam();
        param.cls = cls;
        param.data = data;
        param.type = FragmentParam.TYPE.REPLACE;
        param.addToBackStack = false;

        processFragement(param);
    }

    protected String getFragmentTag(FragmentParam param) {

        StringBuilder sb = new StringBuilder(param.cls.toString());
        return sb.toString();
    }

    private void processFragement(FragmentParam param) {
        int containerId = getFragmentContainerId();
        if(containerId <= 0)
            return;
        Class<?> cls = param.cls;
        if(cls == null) {
            return;
        }
        try {
            String fragmentTag = getFragmentTag(param);
            BaseCoreFragment fragment = (BaseCoreFragment)getFragmentManager().findFragmentByTag(fragmentTag);
            if(fragment == null) {
                fragment = (BaseCoreFragment)cls.newInstance();
            }
            fragment.onComeIn(param.data);

            FragmentTransaction ft = getFragmentManager().beginTransaction();
            if(param.type == FragmentParam.TYPE.ADD) {
                if(fragment.isAdded()) {
                    ft.show(fragment);
                } else {
                    ft.add(containerId, fragment, fragmentTag);
                }
            } else {
                ft.replace(containerId, fragment, fragmentTag);
            }

            currentFragment = fragment;
            if(param.addToBackStack) {
                ft.addToBackStack(fragmentTag);
            }
            ft.commitAllowingStateLoss();

        } catch(InstantiationException e) {
            DebugLog.i(e.getMessage());
        } catch(IllegalAccessException e) {
            DebugLog.i(e.getMessage());
        }
    }

    public void goToFragment(Class<?> cls, Bundle data) {
        if(cls == null) {
            return;
        }
        BaseCoreFragment fragment = (BaseCoreFragment)getFragmentManager().findFragmentByTag(cls.toString());
        if(fragment != null) {
            currentFragment = fragment;
            fragment.onBack(data);
        }
        getFragmentManager().popBackStackImmediate(cls.toString(), 0);
    }

    public void popTopFragment(Bundle data) {
        FragmentManager fm = getFragmentManager();
        fm.popBackStackImmediate();
        currentFragment = null;
        int cnt = fm.getBackStackEntryCount();
        String name = fm.getBackStackEntryAt(cnt - 1).getName();
        currentFragment = (BaseCoreFragment)fm.findFragmentByTag(name);
        currentFragment.onBack(data);
    }

    public void popToRoot(Bundle data) {
        FragmentManager fm = getFragmentManager();
        while(fm.getBackStackEntryCount() > 1) {
            fm.popBackStackImmediate();
        }
        popTopFragment(data);
    }

}
